/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package graphviewer;
import java.util.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;

/**
 *
 * @author Sabitov Artem
 * @since 08.07.2013
 */
class DebugMouse extends JPanel implements MouseMotionListener {
	private int posX = 0;
	private int posY = 0;
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		Font font = new Font("Arial", 1, 10); 
		g2d.setFont(font);
		// смещение по х = 8, по y = 30. учитываем рамку воруг 
		g2d.drawString(/*"debug: " + */"X: " + (posX - 8) + " Y: " + (posY - 30), 4, 8);//4, 726);
		setBounds(0, 720, 80, 10);
	}
	
	public DebugMouse(JFrame objFrame) {
		setBackground(Color.RED);
		setOpaque(Debug.getStateDebugComponent());
		setBounds(0, 720, 80, 10);
		initMouseListener(objFrame);
	}
	
	private void initMouseListener(JFrame objFrame) {
		objFrame.addMouseMotionListener(this);
	}
	
	@Override
	public void mouseDragged(MouseEvent me) {
	}

	@Override
	public void mouseMoved(MouseEvent me) {
		posX = me.getX();
		posY = me.getY();
		repaint();
	}
}

class DebugInfo extends JPanel {
	private int posX = 4;
	private int posY = 0;
	private int step = 14;
	private String sDbg = "off";
	private ArrayList<String> debugBuffer;
	
	private void getDebugInfo() {
		debugBuffer = new ArrayList<>();
		debugBuffer.add("debug: " + sDbg); // состояние дебагера
		debugBuffer.add("node count: " + Node.getNodeCount()); // количество нодов
		debugBuffer.add("force in: " + CalcDistance.FORCE_IN); // сила притяжения
		debugBuffer.add("force out: " + CalcDistance.FORCE_OUT); // сила отталкивания
		debugBuffer.add("distance min: " + CalcDistance.MIN_DISTANCE); //
		debugBuffer.add("distance max: " + CalcDistance.MAX_DISTANCE); //
		debugBuffer.add("step: " + (CalcDistance.MAX_DISTANCE - CalcDistance.MIN_DISTANCE)); //
		debugBuffer.add("time factor: " + (1 / CalcDistance.TIME_FACTOR)); //
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		Font font = new Font("Arial", 0, 11); 
		g2d.setFont(font);
		for(int i = 0; i < debugBuffer.size(); i++) {
			g2d.drawString(debugBuffer.get(i), posX, posY + step + step * i);
			g2d.drawLine(posX, posY + step + step * i + 3, posX + GraphViewer.FRAME_WIDTH / 4, posY + step + step * i);
		}
	}
	
	public DebugInfo() {
		sDbg = "on";
		getDebugInfo();
		setBackground(Color.BLUE);
		setOpaque(Debug.getStateDebugComponent());
		setBounds(0, 0, 236, 296);
	}
	
	public void update() {
		debugBuffer.clear();
		this.getDebugInfo();
		repaint();
	}
}

class DebugLog extends JPanel {
	private int posX = 4;
	private int posY = 0;
	private String log = "log: ";
	private String sDbg = "on";
	private int step = 14;
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		Font font = new Font("Arial", 0, 11);
		g2d.setFont(font);
		for(int i = 0; i < 20; i++) {
			g2d.drawString(log + "hello wolrd", posX, posY + step + step * i);
			g2d.drawLine(posX, posY + step + step * i + 3, posX + GraphViewer.FRAME_WIDTH / 4, posY + step + step * i);

		}
		setBounds(0, 300, 236, 412);
	}
	
	public DebugLog() {
		setBackground(Color.YELLOW);
		setOpaque(Debug.getStateDebugComponent());
	}
}

public class Debug extends JFrame {
	private static boolean bDebugComponent = false;
	private DebugInfo dbgInfo;
	private DebugLog dbgLog;
	
	public Debug() {
		setLocation(GraphViewer.FRAME_WIDTH + GraphViewer.FRAME_LOCATION_X + 4, GraphViewer.FRAME_LOCATION_Y);
		setTitle("Debugger");
		setSize(GraphViewer.FRAME_WIDTH / 4, GraphViewer.FRAME_HEIGHT);
		getContentPane().setBackground(Color.GRAY);
		setVisible(true);
		
		dbgInfo = new DebugInfo();
		dbgLog = new DebugLog();
		
		add(new DebugMouse(this));
		add(dbgInfo);
		add(dbgLog);
	}
	
	public void updateDebug() {
		dbgInfo.update();
	}
	
	public static boolean getStateDebugComponent() {
		return bDebugComponent;
	}
}
